using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem.XR;

public class Player : MonoBehaviour
{
    public Core Core_ { get; private set; }
    public Animator Anim { get; private set; }
    public PlayerController Controller { get; private set; }
    public Rigidbody2D Rb { get; private set; }
    public Weapon weapon { get; set; }

    public PlayerStateMachine StateMachine { get; private set; }
    public PlayerIdleState IdleState { get; private set; }
    public PlayerMoveState MoveState { get; private set; }
    public PlayerAttackState AttackState { get; set; }

    private void Awake()
    {
        Controller = GetComponent<PlayerController>();
        Anim = GetComponent<Animator>();
        Rb = GetComponent<Rigidbody2D>();
        Core_ = GetComponentInChildren<Core>();
        Core_.player = this;
        weapon = GetComponentInChildren<Weapon>();
        weapon.SetCore(Core_);
        StateMachine = new PlayerStateMachine();

        IdleState = new PlayerIdleState(this, StateMachine, "idle");
        MoveState = new PlayerMoveState(this, StateMachine, "move");
        AttackState = new PlayerAttackState(this, StateMachine, "attack", weapon);
    }

    private void Start()
    {
        StateMachine.Init(IdleState);
        Controller.OnInteract += Core_.TriggerInteraction;
        Core_.UpdateWeapon += Core_.HandleWeaponChanged;
    }

    private void OnDisable()
    {
        Core_.UpdateWeapon -= Core_.HandleWeaponChanged;
    }

    private void Update()
    {
        Core_.LogicUpdate();
        StateMachine.CurrentState.LogicUpdate();
    }

    private void FixedUpdate()
    {
        StateMachine.CurrentState.PhysicsUpdate();
    }
}
